说明:

  1. 参照于http://cookdata.cn/note/view_static_note/84230091c4104946ab3d22dd675df58d/
  2. 前面部分爬取数据
  3. 后面部分可视化
In [ ]:
# 查看当前挂载的数据集目录, 该目录下的变更重启环境后会自动还原
# View dataset directory. This directory will be recovered automatically after resetting environment. 
!ls /home/aistudio/data
In [ ]:
# 查看工作区文件, 该目录下的变更将会持久保存. 请及时清理不必要的文件, 避免加载过慢.
# View personal work directory. All changes under this directory will be kept even after reset. Please clean unnecessary files in time to speed up environment loading.
!ls /home/aistudio/work
In [2]:
# 如果需要进行持久化安装, 需要使用持久化路径, 如下方代码示例:
# If a persistence installation is required, you need to use the persistence path as the following:
!mkdir /home/aistudio/external-libraries
!pip install pyecharts -t /home/aistudio/external-libraries
In [3]:
# 同时添加如下代码, 这样每次环境(kernel)启动的时候只要运行下方代码即可:
# Also add the following code, so that every time the environment (kernel) starts, just run the following code:
import sys
sys.path.append('/home/aistudio/external-libraries')
In [1]:
#请求数据,导入使用的一些包
import requests
import pandas as pd
import time
import json
pd.set_option('max_rows', 500)
In [ ]:
#设置请求开头,伪装成浏览器
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
}

#发起请求,将找到的第一个数据源作为请求目标
url = 'https://c.m.163.com/ug/api/wuhan/app/data/list-total'
r = requests.get(url, headers=headers)  # 使用requests发起请求

#根据网页内容,状态为200为成功
print(r.status_code)
print(type(r.text))
print(len(r.text))

#将数据转为json格式
data_json = json.loads(r.text)
data_json.keys()
#注意,从原网页上可以看出数据保存在data中
200
<class 'str'>
261176
Out[ ]:
dict_keys(['reqId', 'code', 'msg', 'data', 'timestamp'])
In [ ]:
#上面的keys中的data保存着需要的数据
data = data_json['data']
data.keys()
Out[ ]:
dict_keys(['chinaTotal', 'chinaDayList', 'lastUpdateTime', 'overseaLastUpdateTime', 'areaTree'])
keys details
chinaTotal 全国当日数据
chinaDayList 全国历史数据
lastUpdateTime 更新时间
overseaLastUpdateTime 海外更新时间
areaTree 世界各地实时数据
In [ ]:
#全国各省实时数据爬取
data_province = data['areaTree'][2]['children']
#数据为列表格式
data_province[0].keys()
Out[ ]:
dict_keys(['today', 'total', 'extData', 'name', 'id', 'lastUpdateTime', 'children'])
keys details
today 省当日数据
total 省当日累计数据
extData 无数据
name 省名
id 省编号
lastUpdateTime 更新时间
children 子数据
In [ ]:
#遍历查看各省名称、更新时间
for i in range(len(data_province)):
    print(data_province[i]['name'], data_province[i]['lastUpdateTime'])
    if i == 10:
        break
湖北 2020-05-25 08:58:00
广东 2020-05-25 08:24:28
河南 2020-05-25 08:58:29
浙江 2020-05-25 09:32:02
香港 2020-05-25 08:53:18
湖南 2020-05-25 08:25:09
安徽 2020-05-25 08:58:22
黑龙江 2020-05-25 08:23:19
江西 2020-05-25 09:03:43
山东 2020-05-25 08:33:38
上海 2020-05-25 14:16:53

不需要采集的数据:children、extData。 需要采集的数据:由于数据中today和total嵌套着字典,因此不能直接获取,对于id、lastUpdateTime、name、可以直接取出为一个数据,today为一个数据,total为一个数据,最后三个数据合并为一个数据。

In [ ]:
# 获取id、lastUpdateTime、name
info = pd.DataFrame(data_province)[['id','lastUpdateTime','name']]
info.head()
Out[ ]:
id lastUpdateTime name
0 420000 2020-05-25 08:58:00 湖北
1 440000 2020-05-25 08:24:28 广东
2 410000 2020-05-25 08:58:29 河南
3 330000 2020-05-25 09:32:02 浙江
4 810000 2020-05-25 08:53:18 香港
In [ ]:
# 将提取数据的方法封装为函数
def get_data(data,info_list):
    info = pd.DataFrame(data)[info_list] # 主要信息
    
    today_data = pd.DataFrame([i['today'] for i in data ]) # 生成today的数据
    today_data.columns = ['today_'+i for i in today_data.columns] # 修改列名
    
    total_data = pd.DataFrame([i['total'] for i in data ]) # 生成total的数据
    total_data.columns = ['total_'+i for i in total_data.columns] # 修改列名
    
    return pd.concat([info,total_data,today_data],axis=1) # info、today和total横向合并最终得到汇总的数据
today_province = get_data(data_province,['id','lastUpdateTime','name'])
today_province.head()
Out[ ]:
id lastUpdateTime name total_confirm total_dead total_heal total_input total_severe total_suspect today_confirm today_dead today_heal today_input today_severe today_storeConfirm today_suspect
0 420000 2020-05-25 08:58:00 湖北 68135 4512 63617 0 0 0 0 0 1 0 NaN -1 NaN
1 440000 2020-05-25 08:24:28 广东 1592 8 1581 0 0 0 0 0 1 0 NaN -1 NaN
2 410000 2020-05-25 08:58:29 河南 1276 22 1254 0 0 0 0 0 0 0 NaN 0 NaN
3 330000 2020-05-25 09:32:02 浙江 1268 1 1267 0 0 0 0 0 0 0 NaN 0 NaN
4 810000 2020-05-25 08:53:18 香港 1065 4 1030 0 0 0 0 0 1 0 0.0 -1 0.0
In [ ]:
def save_data(data,name): # 定义保存数据方法
    file_name = name+'_'+time.strftime('%Y_%m_%d',time.localtime(time.time()))+'.csv'
    data.to_csv(file_name,index=None,encoding='utf_8_sig')
    print(file_name+' 保存成功!')
save_data(today_province,'today_province')
time.strftime('%Y_%m_%d',time.localtime(time.time()))
today_province_2020_05_25.csv 保存成功!
Out[ ]:
'2020_05_25'
In [ ]:
#开始提取世界各国实时数据
areaTree = data['areaTree'] # 取出areaTree

#瞅瞅第一个国家
areaTree[0]
#和之前的差不多
Out[ ]:
{'today': {'confirm': 3,
  'suspect': 0,
  'heal': 3,
  'dead': 0,
  'severe': 0,
  'storeConfirm': 0},
 'total': {'confirm': 1051,
  'suspect': 0,
  'heal': 917,
  'dead': 48,
  'severe': 0},
 'extData': {},
 'name': '突尼斯',
 'id': '9577772',
 'lastUpdateTime': '2020-05-25 14:12:58',
 'children': []}
In [ ]:
for i in range(len(areaTree)):  # 查看各国家名称和更新时间
    print(areaTree[i]['name'],areaTree[i]['lastUpdateTime'])
    if i == 10:
        break
突尼斯 2020-05-25 14:12:58
塞尔维亚 2020-05-25 13:01:05
中国 2020-05-25 14:17:07
日本 2020-05-25 00:00:31
泰国 2020-05-25 13:19:03
新加坡 2020-05-25 12:53:07
韩国 2020-05-25 09:25:09
澳大利亚 2020-05-25 13:07:57
德国 2020-05-25 11:38:05
美国 2020-05-25 07:36:34
马来西亚 2020-05-25 13:07:42
In [ ]:
#从areaTree中提取每个国家的实时数据
today_world = get_data(areaTree,['id','lastUpdateTime','name'])
today_world.head()
Out[ ]:
id lastUpdateTime name total_confirm total_dead total_heal total_input total_severe total_suspect today_confirm today_dead today_heal today_input today_severe today_storeConfirm today_suspect
0 9577772 2020-05-25 14:12:58 突尼斯 1051 48 917 NaN 0 0 3.0 0.0 3.0 NaN 0.0 0.0 0.0
1 9507896 2020-05-25 13:01:05 塞尔维亚 11159 238 5857 NaN 0 0 67.0 0.0 158.0 NaN 0.0 -91.0 0.0
2 0 2020-05-25 14:17:07 中国 84536 4645 79762 1724.0 7 6 11.0 0.0 16.0 11.0 0.0 -5.0 0.0
3 1 2020-05-25 00:00:31 日本 17323 851 14010 NaN 0 0 NaN NaN NaN NaN NaN NaN NaN
4 2 2020-05-25 13:19:03 泰国 3042 57 2928 NaN 0 0 2.0 1.0 12.0 NaN 0.0 -11.0 0.0
In [ ]:
#保存世界各国的
save_data(today_world,'today_world')
today_world_2020_05_25.csv 保存成功!

历史数据的爬取

In [ ]:
chinaDayList = data['chinaDayList']
chinaDayList[0]
Out[ ]:
{'date': '2020-01-20',
 'today': {'confirm': 291,
  'suspect': 27,
  'heal': 25,
  'dead': 6,
  'severe': 0,
  'storeConfirm': None},
 'total': {'confirm': 291,
  'suspect': 54,
  'heal': 25,
  'dead': 6,
  'severe': 0,
  'input': 0},
 'extData': None,
 'lastUpdateTime': None}
In [ ]:
alltime_China = get_data(chinaDayList,['date','lastUpdateTime'])
alltime_China.head()
Out[ ]:
date lastUpdateTime total_confirm total_dead total_heal total_input total_severe total_suspect today_confirm today_dead today_heal today_input today_severe today_storeConfirm today_suspect
0 2020-01-20 None 291 6 25 0 0 54 291 6 25 NaN 0 None 27
1 2020-01-21 None 440 9 25 0 102 37 149 3 -25 NaN 0 None 26
2 2020-01-22 None 571 17 28 0 95 393 131 8 28 NaN 0 None 257
3 2020-01-23 None 830 25 34 0 177 1072 259 8 6 NaN 0 None 680
4 2020-01-24 None 1287 41 38 0 237 1965 444 16 4 NaN 0 None 1118
In [ ]:
#全国历史数据
save_data(alltime_China,'alltime_China')
alltime_China_2020_05_25.csv 保存成功!
In [ ]:
#省历史数据
province_dict = {num:name for num,name in zip(today_province['id'],today_province['name'])}
count = 0
for i in province_dict:
    print(i,province_dict[i])
    count += 1
    if count == 10:
        break
420000 湖北
440000 广东
410000 河南
330000 浙江
810000 香港
430000 湖南
340000 安徽
230000 黑龙江
360000 江西
370000 山东
In [ ]:
#得到每个省的数据并合并
start = time.time()
for province_id in province_dict: # 遍历各省编号
    
    try:
        # 按照省编号访问每个省的数据地址,并获取json数据
        url = 'https://c.m.163.com/ug/api/wuhan/app/data/list-by-area-code?areaCode='+province_id
        r = requests.get(url, headers=headers)
        data_json = json.loads(r.text)
        
        # 提取各省数据,然后写入各省名称
        province_data = get_data(data_json['data']['list'],['date'])
        province_data['name'] = province_dict[province_id]
        
        # 合并数据
        if province_id == '420000':
            alltime_province = province_data
        else:
            alltime_province = pd.concat([alltime_province,province_data])
            
        print('-'*20,province_dict[province_id],'成功',
              province_data.shape,alltime_province.shape,
              ',累计耗时:',round(time.time()-start),'-'*20)
        
        # 设置延迟等待
        time.sleep(20)
        
    except:
        print('-'*20,province_dict[province_id],'wrong','-'*20)
-------------------- 湖北 成功 (126, 15) (126, 15) ,累计耗时: 0 --------------------
-------------------- 广东 成功 (123, 15) (249, 15) ,累计耗时: 20 --------------------
-------------------- 河南 成功 (125, 15) (374, 15) ,累计耗时: 41 --------------------
-------------------- 浙江 成功 (122, 15) (496, 15) ,累计耗时: 61 --------------------
-------------------- 香港 成功 (125, 15) (621, 15) ,累计耗时: 81 --------------------
-------------------- 湖南 成功 (124, 15) (745, 15) ,累计耗时: 101 --------------------
-------------------- 安徽 成功 (124, 15) (869, 15) ,累计耗时: 121 --------------------
-------------------- 黑龙江 成功 (123, 15) (992, 15) ,累计耗时: 142 --------------------
-------------------- 江西 成功 (123, 15) (1115, 15) ,累计耗时: 162 --------------------
-------------------- 山东 成功 (124, 15) (1239, 15) ,累计耗时: 182 --------------------
-------------------- 上海 成功 (126, 15) (1365, 15) ,累计耗时: 202 --------------------
-------------------- 江苏 成功 (124, 15) (1489, 15) ,累计耗时: 222 --------------------
-------------------- 北京 成功 (125, 15) (1614, 15) ,累计耗时: 243 --------------------
-------------------- 重庆 成功 (125, 15) (1739, 15) ,累计耗时: 263 --------------------
-------------------- 四川 成功 (125, 15) (1864, 15) ,累计耗时: 283 --------------------
-------------------- 台湾 成功 (126, 15) (1990, 15) ,累计耗时: 303 --------------------
-------------------- 福建 成功 (123, 15) (2113, 15) ,累计耗时: 323 --------------------
-------------------- 河北 成功 (122, 15) (2235, 15) ,累计耗时: 344 --------------------
-------------------- 陕西 成功 (123, 15) (2358, 15) ,累计耗时: 364 --------------------
-------------------- 广西 成功 (124, 15) (2482, 15) ,累计耗时: 384 --------------------
-------------------- 内蒙古 成功 (122, 15) (2604, 15) ,累计耗时: 404 --------------------
-------------------- 山西 成功 (122, 15) (2726, 15) ,累计耗时: 424 --------------------
-------------------- 天津 成功 (122, 15) (2848, 15) ,累计耗时: 445 --------------------
-------------------- 云南 成功 (124, 15) (2972, 15) ,累计耗时: 465 --------------------
-------------------- 海南 成功 (123, 15) (3095, 15) ,累计耗时: 485 --------------------
-------------------- 吉林 成功 (123, 15) (3218, 15) ,累计耗时: 505 --------------------
-------------------- 辽宁 成功 (124, 15) (3342, 15) ,累计耗时: 525 --------------------
-------------------- 贵州 成功 (125, 15) (3467, 15) ,累计耗时: 546 --------------------
-------------------- 甘肃 成功 (120, 15) (3587, 15) ,累计耗时: 566 --------------------
-------------------- 新疆 成功 (125, 15) (3712, 15) ,累计耗时: 586 --------------------
-------------------- 宁夏 成功 (122, 15) (3834, 15) ,累计耗时: 606 --------------------
-------------------- 澳门 成功 (125, 15) (3959, 15) ,累计耗时: 627 --------------------
-------------------- 青海 成功 (120, 15) (4079, 15) ,累计耗时: 648 --------------------
-------------------- 西藏 成功 (124, 15) (4203, 15) ,累计耗时: 668 --------------------
In [ ]:
alltime_province.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 4203 entries, 0 to 123
Data columns (total 15 columns):
date                  4203 non-null object
total_confirm         4203 non-null int64
total_dead            4203 non-null int64
total_heal            4203 non-null int64
total_input           4203 non-null int64
total_severe          4203 non-null int64
total_suspect         4203 non-null int64
today_confirm         4203 non-null int64
today_dead            4203 non-null int64
today_heal            4203 non-null int64
today_input           4169 non-null float64
today_severe          266 non-null object
today_storeConfirm    0 non-null object
today_suspect         494 non-null float64
name                  4203 non-null object
dtypes: float64(2), int64(9), object(4)
memory usage: 525.4+ KB
In [ ]:
save_data(alltime_province,'alltime_province')
alltime_province_2020_05_25.csv 保存成功!

世界各国历史数据爬取

In [ ]:
country_dict = {key:value for key,value in zip(today_world['id'], today_world['name'])}
# 查看前十个内容
count = 0
for i in country_dict:
    print(i,country_dict[i])
    count += 1
    if count == 10:
        break
9577772 突尼斯
9507896 塞尔维亚
0 中国
1 日本
2 泰国
3 新加坡
4 韩国
5 澳大利亚
6 德国
7 美国
In [ ]:
start = time.time()
for country_id in country_dict: # 遍历每个国家的编号
    
    try:
        # 按照编号访问每个国家的数据地址,并获取json数据
        url = 'https://c.m.163.com/ug/api/wuhan/app/data/list-by-area-code?areaCode='+country_id
        r = requests.get(url, headers=headers)
        json_data = json.loads(r.text)
        
        # 生成每个国家的数据
        country_data = get_data(json_data['data']['list'],['date'])
        country_data['name'] = country_dict[country_id]

        # 数据叠加
        if country_id == '9577772':
            alltime_world = country_data
        else:
            alltime_world = pd.concat([alltime_world,country_data])
            
        print('-'*20,country_dict[country_id],'成功',country_data.shape,alltime_world.shape,
              ',累计耗时:',round(time.time()-start),'-'*20)
        
        time.sleep(25)

    except:
        print('-'*20,country_dict[country_id],'wrong','-'*20)
print('end')
-------------------- 突尼斯 成功 (71, 15) (71, 15) ,累计耗时: 0 --------------------
-------------------- 塞尔维亚 成功 (75, 15) (146, 15) ,累计耗时: 25 --------------------
-------------------- 中国 成功 (111, 15) (257, 15) ,累计耗时: 51 --------------------
-------------------- 日本 成功 (113, 15) (370, 15) ,累计耗时: 76 --------------------
-------------------- 泰国 成功 (86, 15) (456, 15) ,累计耗时: 101 --------------------
-------------------- 新加坡 成功 (103, 15) (559, 15) ,累计耗时: 126 --------------------
-------------------- 韩国 成功 (126, 15) (685, 15) ,累计耗时: 151 --------------------
-------------------- 澳大利亚 成功 (85, 15) (770, 15) ,累计耗时: 177 --------------------
-------------------- 德国 成功 (92, 15) (862, 15) ,累计耗时: 202 --------------------
-------------------- 美国 成功 (95, 15) (957, 15) ,累计耗时: 227 --------------------
-------------------- 马来西亚 成功 (89, 15) (1046, 15) ,累计耗时: 252 --------------------
-------------------- 越南 成功 (61, 15) (1107, 15) ,累计耗时: 277 --------------------
-------------------- 圣巴泰勒米 成功 (13, 15) (1120, 15) ,累计耗时: 303 --------------------
-------------------- 肯尼亚 成功 (64, 15) (1184, 15) ,累计耗时: 328 --------------------
-------------------- 伊朗 成功 (97, 15) (1281, 15) ,累计耗时: 353 --------------------
-------------------- 以色列 成功 (89, 15) (1370, 15) ,累计耗时: 378 --------------------
-------------------- 毛利亚尼亚 成功 (8, 15) (1378, 15) ,累计耗时: 403 --------------------
-------------------- 黎巴嫩 成功 (72, 15) (1450, 15) ,累计耗时: 429 --------------------
-------------------- 克罗地亚 成功 (73, 15) (1523, 15) ,累计耗时: 454 --------------------
-------------------- 奥地利 成功 (83, 15) (1606, 15) ,累计耗时: 479 --------------------
-------------------- 瑞士 成功 (89, 15) (1695, 15) ,累计耗时: 504 --------------------
-------------------- 希腊 成功 (79, 15) (1774, 15) ,累计耗时: 530 --------------------
-------------------- 毛里求斯 成功 (52, 15) (1826, 15) ,累计耗时: 555 --------------------
-------------------- 爱沙尼亚 成功 (73, 15) (1899, 15) ,累计耗时: 580 --------------------
-------------------- 北马其顿 成功 (62, 15) (1961, 15) ,累计耗时: 605 --------------------
-------------------- 厄立特里亚 成功 (2, 15) (1963, 15) ,累计耗时: 630 --------------------
-------------------- 白俄罗斯 成功 (68, 15) (2031, 15) ,累计耗时: 656 --------------------
-------------------- 立陶宛 成功 (70, 15) (2101, 15) ,累计耗时: 681 --------------------
-------------------- 阿塞拜疆 成功 (68, 15) (2169, 15) ,累计耗时: 706 --------------------
-------------------- 美属维尔京群岛 成功 (14, 15) (2183, 15) ,累计耗时: 731 --------------------
-------------------- 蒙古 成功 (39, 15) (2222, 15) ,累计耗时: 756 --------------------
-------------------- 乌克兰 成功 (72, 15) (2294, 15) ,累计耗时: 782 --------------------
-------------------- 波兰 成功 (77, 15) (2371, 15) ,累计耗时: 807 --------------------
-------------------- 波斯尼亚 成功 (14, 15) (2385, 15) ,累计耗时: 832 --------------------
-------------------- 波黑 成功 (65, 15) (2450, 15) ,累计耗时: 857 --------------------
-------------------- 蒙特塞拉特 成功 (16, 15) (2466, 15) ,累计耗时: 882 --------------------
-------------------- 南非 成功 (78, 15) (2544, 15) ,累计耗时: 908 --------------------
-------------------- 布隆迪 成功 (21, 15) (2565, 15) ,累计耗时: 933 --------------------
-------------------- 南苏丹 成功 (27, 15) (2592, 15) ,累计耗时: 958 --------------------
-------------------- 马耳他 成功 (50, 15) (2642, 15) ,累计耗时: 983 --------------------
-------------------- 摩尔多瓦 成功 (66, 15) (2708, 15) ,累计耗时: 1008 --------------------
-------------------- 保加利亚 成功 (71, 15) (2779, 15) ,累计耗时: 1034 --------------------
-------------------- 孟加拉国 成功 (63, 15) (2842, 15) ,累计耗时: 1059 --------------------
-------------------- 阿尔巴尼亚 成功 (67, 15) (2909, 15) ,累计耗时: 1084 --------------------
-------------------- 巴勒斯坦 成功 (63, 15) (2972, 15) ,累计耗时: 1109 --------------------
-------------------- 科摩罗 成功 (16, 15) (2988, 15) ,累计耗时: 1134 --------------------
-------------------- 阿富汗 成功 (68, 15) (3056, 15) ,累计耗时: 1160 --------------------
-------------------- 沙特阿拉伯 成功 (78, 15) (3134, 15) ,累计耗时: 1185 --------------------
-------------------- 新西兰 成功 (74, 15) (3208, 15) ,累计耗时: 1210 --------------------
-------------------- 塔吉克斯坦 成功 (21, 15) (3229, 15) ,累计耗时: 1235 --------------------
-------------------- 泽西岛 成功 (10, 15) (3239, 15) ,累计耗时: 1260 --------------------
-------------------- 叙利亚 成功 (30, 15) (3269, 15) ,累计耗时: 1285 --------------------
-------------------- 巴拿马 成功 (63, 15) (3332, 15) ,累计耗时: 1311 --------------------
-------------------- 古巴 成功 (51, 15) (3383, 15) ,累计耗时: 1336 --------------------
-------------------- 尼日利亚 成功 (71, 15) (3454, 15) ,累计耗时: 1361 --------------------
-------------------- 摩洛哥 成功 (77, 15) (3531, 15) ,累计耗时: 1386 --------------------
-------------------- 塞内加尔 成功 (73, 15) (3604, 15) ,累计耗时: 1412 --------------------
-------------------- 老挝 成功 (24, 15) (3628, 15) ,累计耗时: 1437 --------------------
-------------------- 巴哈马 成功 (28, 15) (3656, 15) ,累计耗时: 1462 --------------------
-------------------- 马约特岛 成功 (20, 15) (3676, 15) ,累计耗时: 1487 --------------------
-------------------- 斯洛文尼亚 成功 (67, 15) (3743, 15) ,累计耗时: 1513 --------------------
-------------------- 卢森堡 成功 (71, 15) (3814, 15) ,累计耗时: 1538 --------------------
-------------------- 爱尔兰 成功 (80, 15) (3894, 15) ,累计耗时: 1563 --------------------
-------------------- 厄瓜多尔 成功 (68, 15) (3962, 15) ,累计耗时: 1588 --------------------
-------------------- 捷克 成功 (81, 15) (4043, 15) ,累计耗时: 1613 --------------------
-------------------- 索马里 成功 (2, 15) (4045, 15) ,累计耗时: 1639 --------------------
-------------------- 匈牙利 成功 (79, 15) (4124, 15) ,累计耗时: 1664 --------------------
-------------------- 法属圭亚那 成功 (27, 15) (4151, 15) ,累计耗时: 1689 --------------------
-------------------- 多哥共和国 成功 (47, 15) (4198, 15) ,累计耗时: 1714 --------------------
-------------------- 哥斯达黎加 成功 (57, 15) (4255, 15) ,累计耗时: 1739 --------------------
-------------------- 文莱 成功 (45, 15) (4300, 15) ,累计耗时: 1765 --------------------
-------------------- 法罗群岛 成功 (26, 15) (4326, 15) ,累计耗时: 1790 --------------------
-------------------- 马提尼克岛 成功 (26, 15) (4352, 15) ,累计耗时: 1815 --------------------
-------------------- 荷兰 成功 (88, 15) (4440, 15) ,累计耗时: 1840 --------------------
-------------------- 巴西 成功 (83, 15) (4523, 15) ,累计耗时: 1865 --------------------
-------------------- 洪都拉斯 成功 (47, 15) (4570, 15) ,累计耗时: 1891 --------------------
-------------------- 乌拉圭 成功 (61, 15) (4631, 15) ,累计耗时: 1916 --------------------
-------------------- 秘鲁 成功 (73, 15) (4704, 15) ,累计耗时: 1941 --------------------
-------------------- 智利 成功 (80, 15) (4784, 15) ,累计耗时: 1966 --------------------
-------------------- 格陵兰 成功 (18, 15) (4802, 15) ,累计耗时: 1991 --------------------
-------------------- 圣巴托洛谬岛 成功 (14, 15) (4816, 15) ,累计耗时: 2017 --------------------
-------------------- 马尔代夫 成功 (48, 15) (4864, 15) ,累计耗时: 2042 --------------------
-------------------- 委内瑞拉 成功 (45, 15) (4909, 15) ,累计耗时: 2067 --------------------
-------------------- 毛里塔尼亚 成功 (33, 15) (4942, 15) ,累计耗时: 2092 --------------------
-------------------- 纳米比亚 成功 (26, 15) (4968, 15) ,累计耗时: 2118 --------------------
-------------------- 法属留尼汪岛 成功 (19, 15) (4987, 15) ,累计耗时: 2143 --------------------
-------------------- 波多黎各 成功 (27, 15) (5014, 15) ,累计耗时: 2168 --------------------
-------------------- 加纳 成功 (52, 15) (5066, 15) ,累计耗时: 2193 --------------------
-------------------- 赤道几内亚 成功 (38, 15) (5104, 15) ,累计耗时: 2218 --------------------
-------------------- 几内亚 成功 (57, 15) (5161, 15) ,累计耗时: 2244 --------------------
-------------------- 卢旺达 成功 (58, 15) (5219, 15) ,累计耗时: 2269 --------------------
-------------------- 格林纳达 成功 (22, 15) (5241, 15) ,累计耗时: 2294 --------------------
-------------------- 斯威士兰 成功 (40, 15) (5281, 15) ,累计耗时: 2319 --------------------
-------------------- 坦桑尼亚 成功 (39, 15) (5320, 15) ,累计耗时: 2344 --------------------
-------------------- 贝宁 成功 (34, 15) (5354, 15) ,累计耗时: 2370 --------------------
-------------------- 刚果(金) 成功 (64, 15) (5418, 15) ,累计耗时: 2395 --------------------
-------------------- 中非共和国 成功 (33, 15) (5451, 15) ,累计耗时: 2420 --------------------
-------------------- 利比里亚 成功 (35, 15) (5486, 15) ,累计耗时: 2445 --------------------
-------------------- 索马里 成功 (43, 15) (5529, 15) ,累计耗时: 2470 --------------------
-------------------- 塞拉利昂 成功 (39, 15) (5568, 15) ,累计耗时: 2496 --------------------
-------------------- 乍得 成功 (32, 15) (5600, 15) ,累计耗时: 2521 --------------------
-------------------- 赞比亚 成功 (50, 15) (5650, 15) ,累计耗时: 2546 --------------------
-------------------- 巴巴多斯 成功 (30, 15) (5680, 15) ,累计耗时: 2571 --------------------
-------------------- 马里 成功 (53, 15) (5733, 15) ,累计耗时: 2596 --------------------
-------------------- 阿根廷 成功 (76, 15) (5809, 15) ,累计耗时: 2622 --------------------
-------------------- 法属波利尼西亚 成功 (18, 15) (5827, 15) ,累计耗时: 2647 --------------------
-------------------- 巴林 成功 (81, 15) (5908, 15) ,累计耗时: 2672 --------------------
-------------------- 莫桑比克 成功 (44, 15) (5952, 15) ,累计耗时: 2697 --------------------
-------------------- 喀麦隆 成功 (57, 15) (6009, 15) ,累计耗时: 2722 --------------------
-------------------- 乌干达 成功 (54, 15) (6063, 15) ,累计耗时: 2748 --------------------
-------------------- 厄立特里亚 成功 (33, 15) (6096, 15) ,累计耗时: 2773 --------------------
-------------------- 刚果(布) 成功 (37, 15) (6133, 15) ,累计耗时: 2798 --------------------
-------------------- 津巴布韦 成功 (43, 15) (6176, 15) ,累计耗时: 2823 --------------------
-------------------- 丹麦 成功 (86, 15) (6262, 15) ,累计耗时: 2848 --------------------
-------------------- 阿鲁巴 成功 (16, 15) (6278, 15) ,累计耗时: 2873 --------------------
-------------------- 斐济 成功 (22, 15) (6300, 15) ,累计耗时: 2899 --------------------
-------------------- 伯利兹 成功 (23, 15) (6323, 15) ,累计耗时: 2924 --------------------
-------------------- 缅甸 成功 (50, 15) (6373, 15) ,累计耗时: 2949 --------------------
-------------------- 塞浦路斯 成功 (65, 15) (6438, 15) ,累计耗时: 2974 --------------------
-------------------- 关岛 成功 (15, 15) (6453, 15) ,累计耗时: 2999 --------------------
-------------------- 科索沃 成功 (18, 15) (6471, 15) ,累计耗时: 3025 --------------------
-------------------- 圣皮埃尔岛和密克隆岛 成功 (4, 15) (6475, 15) ,累计耗时: 3050 --------------------
-------------------- 吉尔吉斯斯坦 成功 (54, 15) (6529, 15) ,累计耗时: 3075 --------------------
-------------------- 博茨瓦纳 成功 (21, 15) (6550, 15) ,累计耗时: 3100 --------------------
-------------------- 尼日尔 成功 (56, 15) (6606, 15) ,累计耗时: 3125 --------------------
-------------------- 苏里南 成功 (23, 15) (6629, 15) ,累计耗时: 3151 --------------------
-------------------- 佛得角 成功 (38, 15) (6667, 15) ,累计耗时: 3176 --------------------
-------------------- 萨尔瓦多 成功 (37, 15) (6704, 15) ,累计耗时: 3201 --------------------
-------------------- 圭亚那 成功 (42, 15) (6746, 15) ,累计耗时: 3226 --------------------
-------------------- 尼加拉瓜 成功 (22, 15) (6768, 15) ,累计耗时: 3251 --------------------
-------------------- 冈比亚 成功 (22, 15) (6790, 15) ,累计耗时: 3277 --------------------
-------------------- 东帝汶 成功 (22, 15) (6812, 15) ,累计耗时: 3302 --------------------
-------------------- 巴基斯坦 成功 (81, 15) (6893, 15) ,累计耗时: 3327 --------------------
-------------------- 埃及 成功 (80, 15) (6973, 15) ,累计耗时: 3352 --------------------
-------------------- 科威特 成功 (79, 15) (7052, 15) ,累计耗时: 3377 --------------------
-------------------- 斯洛伐克 成功 (68, 15) (7120, 15) ,累计耗时: 3403 --------------------
-------------------- 直布罗陀 成功 (19, 15) (7139, 15) ,累计耗时: 3428 --------------------
-------------------- 摩纳哥 成功 (47, 15) (7186, 15) ,累计耗时: 3453 --------------------
-------------------- 巴拉圭 成功 (50, 15) (7236, 15) ,累计耗时: 3478 --------------------
-------------------- 荷属安的列斯 成功 (13, 15) (7249, 15) ,累计耗时: 3503 --------------------
-------------------- 多米尼克 成功 (21, 15) (7270, 15) ,累计耗时: 3529 --------------------
-------------------- 乌兹别克斯坦 成功 (66, 15) (7336, 15) ,累计耗时: 3554 --------------------
-------------------- 黑山 成功 (50, 15) (7386, 15) ,累计耗时: 3579 --------------------
-------------------- 危地马拉 成功 (42, 15) (7428, 15) ,累计耗时: 3604 --------------------
-------------------- 加蓬 成功 (53, 15) (7481, 15) ,累计耗时: 3629 --------------------
-------------------- 苏丹 成功 (48, 15) (7529, 15) ,累计耗时: 3655 --------------------
-------------------- 利比亚 成功 (39, 15) (7568, 15) ,累计耗时: 3680 --------------------
-------------------- 圣马丁岛 成功 (16, 15) (7584, 15) ,累计耗时: 3705 --------------------
-------------------- 土耳其 成功 (75, 15) (7659, 15) ,累计耗时: 3730 --------------------
-------------------- 巴布亚新几内亚 成功 (19, 15) (7678, 15) ,累计耗时: 3755 --------------------
-------------------- 多米尼加共和国 成功 (54, 15) (7732, 15) ,累计耗时: 3780 --------------------
-------------------- 约旦 成功 (66, 15) (7798, 15) ,累计耗时: 3806 --------------------
-------------------- 亚美尼亚 成功 (72, 15) (7870, 15) ,累计耗时: 3831 --------------------
-------------------- 圣基茨和尼维斯 成功 (22, 15) (7892, 15) ,累计耗时: 3856 --------------------
-------------------- 瓜德罗普 成功 (22, 15) (7914, 15) ,累计耗时: 3881 --------------------
-------------------- 安提瓜和巴布达 成功 (22, 15) (7936, 15) ,累计耗时: 3906 --------------------
-------------------- 玻利维亚 成功 (47, 15) (7983, 15) ,累计耗时: 3932 --------------------
-------------------- 哥伦比亚 成功 (70, 15) (8053, 15) ,累计耗时: 3957 --------------------
-------------------- 圣文森特和格林纳丁斯 成功 (20, 15) (8073, 15) ,累计耗时: 3982 --------------------
-------------------- 圣卢西亚 成功 (23, 15) (8096, 15) ,累计耗时: 4007 --------------------
-------------------- 法国 成功 (91, 15) (8187, 15) ,累计耗时: 4032 --------------------
-------------------- 阿联酋 成功 (80, 15) (8267, 15) ,累计耗时: 4058 --------------------
-------------------- 加拿大 成功 (89, 15) (8356, 15) ,累计耗时: 4083 --------------------
-------------------- 印度 成功 (85, 15) (8441, 15) ,累计耗时: 4108 --------------------
-------------------- 英国 成功 (91, 15) (8532, 15) ,累计耗时: 4133 --------------------
-------------------- 意大利 成功 (115, 15) (8647, 15) ,累计耗时: 4158 --------------------
-------------------- 俄罗斯 成功 (84, 15) (8731, 15) ,累计耗时: 4184 --------------------
-------------------- 菲律宾 成功 (82, 15) (8813, 15) ,累计耗时: 4209 --------------------
-------------------- 芬兰 成功 (85, 15) (8898, 15) ,累计耗时: 4234 --------------------
-------------------- 尼泊尔 成功 (37, 15) (8935, 15) ,累计耗时: 4259 --------------------
-------------------- 葡萄牙 成功 (76, 15) (9011, 15) ,累计耗时: 4284 --------------------
-------------------- 也门 成功 (23, 15) (9034, 15) ,累计耗时: 4310 --------------------
-------------------- 塞舌尔 成功 (22, 15) (9056, 15) ,累计耗时: 4335 --------------------
-------------------- 西班牙 成功 (92, 15) (9148, 15) ,累计耗时: 4360 --------------------
-------------------- 斯里兰卡 成功 (69, 15) (9217, 15) ,累计耗时: 4385 --------------------
-------------------- 阿尔及利亚 成功 (73, 15) (9290, 15) ,累计耗时: 4410 --------------------
-------------------- 柬埔寨 成功 (37, 15) (9327, 15) ,累计耗时: 4436 --------------------
-------------------- 海地 成功 (32, 15) (9359, 15) ,累计耗时: 4461 --------------------
-------------------- 瑞典 成功 (86, 15) (9445, 15) ,累计耗时: 4486 --------------------
-------------------- 特里尼达和多巴哥 成功 (27, 15) (9472, 15) ,累计耗时: 4511 --------------------
-------------------- 吉布提 成功 (49, 15) (9521, 15) ,累计耗时: 4536 --------------------
-------------------- 圣多美与普林西比 成功 (15, 15) (9536, 15) ,累计耗时: 4562 --------------------
-------------------- 布基纳法索 成功 (65, 15) (9601, 15) ,累计耗时: 4587 --------------------
-------------------- 比利时 成功 (87, 15) (9688, 15) ,累计耗时: 4612 --------------------
-------------------- 伊拉克 成功 (88, 15) (9776, 15) ,累计耗时: 4637 --------------------
-------------------- 马拉维 成功 (32, 15) (9808, 15) ,累计耗时: 4662 --------------------
-------------------- 冰岛 成功 (76, 15) (9884, 15) ,累计耗时: 4689 --------------------
-------------------- 几内亚比绍 成功 (37, 15) (9921, 15) ,累计耗时: 4714 --------------------
-------------------- 拉脱维亚 成功 (73, 15) (9994, 15) ,累计耗时: 4739 --------------------
-------------------- 不丹 成功 (23, 15) (10017, 15) ,累计耗时: 4764 --------------------
-------------------- 挪威 成功 (85, 15) (10102, 15) ,累计耗时: 4789 --------------------
-------------------- 印度尼西亚 成功 (78, 15) (10180, 15) ,累计耗时: 4815 --------------------
-------------------- 安哥拉 成功 (36, 15) (10216, 15) ,累计耗时: 4840 --------------------
-------------------- 开曼群岛 成功 (17, 15) (10233, 15) ,累计耗时: 4865 --------------------
-------------------- 埃塞俄比亚 成功 (53, 15) (10286, 15) ,累计耗时: 4890 --------------------
-------------------- 梵蒂冈 成功 (22, 15) (10308, 15) ,累计耗时: 4915 --------------------
-------------------- 科特迪瓦 成功 (64, 15) (10372, 15) ,累计耗时: 4941 --------------------
-------------------- 卡塔尔 成功 (78, 15) (10450, 15) ,累计耗时: 4966 --------------------
-------------------- 莱索托 成功 (8, 15) (10458, 15) ,累计耗时: 4991 --------------------
-------------------- 格鲁吉亚 成功 (73, 15) (10531, 15) ,累计耗时: 5016 --------------------
-------------------- 墨西哥 成功 (78, 15) (10609, 15) ,累计耗时: 5041 --------------------
-------------------- 圣马力诺 成功 (45, 15) (10654, 15) ,累计耗时: 5067 --------------------
-------------------- 哈萨克斯坦 成功 (70, 15) (10724, 15) ,累计耗时: 5092 --------------------
-------------------- 安道尔 成功 (56, 15) (10780, 15) ,累计耗时: 5117 --------------------
-------------------- 牙买加 成功 (42, 15) (10822, 15) ,累计耗时: 5142 --------------------
-------------------- 格恩西岛 成功 (11, 15) (10833, 15) ,累计耗时: 5167 --------------------
-------------------- 罗马尼亚 成功 (79, 15) (10912, 15) ,累计耗时: 5193 --------------------
-------------------- 阿曼 成功 (80, 15) (10992, 15) ,累计耗时: 5218 --------------------
-------------------- 列支敦士登 成功 (28, 15) (11020, 15) ,累计耗时: 5243 --------------------
-------------------- 马达加斯加 成功 (45, 15) (11065, 15) ,累计耗时: 5268 --------------------
end
In [ ]:
print(alltime_world.shape)
save_data(alltime_world,'alltime_world')
(11065, 15)
alltime_world_2020_05_25.csv 保存成功!

到这里为止,数据爬取已经完成了,接下来是数据可视化

In [4]:
import pandas as pd
import pyecharts
import pyecharts.options as opts
from pyecharts.charts import Map
In [5]:
#读取数据
world_data = pd.read_csv('today_world_2020_05_25.csv')
world_data.keys()
Out[5]:
Index(['id', 'lastUpdateTime', 'name', 'total_confirm', 'total_dead',
       'total_heal', 'total_input', 'total_severe', 'total_suspect',
       'today_confirm', 'today_dead', 'today_heal', 'today_input',
       'today_severe', 'today_storeConfirm', 'today_suspect'],
      dtype='object')
In [6]:
#现存确诊人数,并添加到数据中
world_data['today_storeConfirm'] = world_data['total_confirm'] - world_data['total_heal'] - world_data['total_dead']
In [7]:
world_data.head()
Out[7]:
id lastUpdateTime name total_confirm total_dead total_heal total_input total_severe total_suspect today_confirm today_dead today_heal today_input today_severe today_storeConfirm today_suspect
0 9577772 2020-05-25 14:12:58 突尼斯 1051 48 917 NaN 0 0 3.0 0.0 3.0 NaN 0.0 86 0.0
1 9507896 2020-05-25 13:01:05 塞尔维亚 11159 238 5857 NaN 0 0 67.0 0.0 158.0 NaN 0.0 5064 0.0
2 0 2020-05-25 14:17:07 中国 84536 4645 79762 1724.0 7 6 11.0 0.0 16.0 11.0 0.0 129 0.0
3 1 2020-05-25 00:00:31 日本 17323 851 14010 NaN 0 0 NaN NaN NaN NaN NaN 2462 NaN
4 2 2020-05-25 13:19:03 泰国 3042 57 2928 NaN 0 0 2.0 1.0 12.0 NaN 0.0 57 0.0
In [8]:
#中英文对照
contry_name = pd.read_csv('name.csv')
contry_name.head()
Out[8]:
英文 中文
0 Afghanistan 阿富汗
1 Aland Islands 奥兰群岛
2 Albania 阿尔巴尼亚
3 Algeria 阿尔及利亚
4 American Samoa 美属萨摩亚
In [9]:
world_data['eg_name'] = world_data['name'].replace(contry_name['中文'].values ,contry_name['英文'].values)
world_data['eg_name'].head()
Out[9]:
0     Tunisia
1      Serbia
2       China
3       Japan
4    Thailand
Name: eg_name, dtype: object
In [10]:
#提取出我们需要的数据,保存成一个嵌套列表的形式:
heatmap_data = world_data[['eg_name','today_storeConfirm']].values.tolist()
heatmap_data[:10]
Out[10]:
[['Tunisia', 86],
 ['Serbia', 5064],
 ['China', 129],
 ['Japan', 2462],
 ['Thailand', 57],
 ['Singapore', 16717],
 ['Korea (South)', 713],
 ['Australia', 504],
 ['Germany', 11657],
 ['United States', 1135434]]
In [11]:
#接下来我们开始绘图,首先初始化类对象Map,并调用add方法添加绘图基本配置:
map_ = Map().add(series_name = "今日现存确诊人数", # 设置提示框标签
                 data_pair = heatmap_data, # 输入数据
                 maptype = "world", # 设置地图类型为世界地图
                 is_map_symbol_show = False # 不显示标记点
                ) 

# 设置系列配置项
map_.set_series_opts(label_opts=opts.LabelOpts(is_show=False))  # 不显示国家(标签)名称 
# 设置全局配置项
map_.set_global_opts(title_opts = opts.TitleOpts(title="世界各国今日现存确诊人数地图"), # 设置图标题
                     # 设置视觉映射配置项
                     visualmap_opts = opts.VisualMapOpts(pieces=[ # 自定义分组的分点和颜色
                                                               {"min": 10000,"color":"#800000"}, # 栗色
                                                               {"min": 5000, "max": 9999, "color":"#B22222"}, # 耐火砖
                                                               {"min": 999, "max": 4999,"color":"#CD5C5C"}, # 印度红
                                                               {"min": 100, "max": 999, "color":"#BC8F8F"}, # 玫瑰棕色
                                                               {"max": 99, "color":"#FFE4E1"}, # 薄雾玫瑰
                                                              ], 
                     is_piecewise = True))  # 显示分段式图例
# 在notebook中进行渲染
map_.render_notebook()
Out[11]:

世界国家累计死亡人数玫瑰图

In [12]:
#先筛选死亡大于1000的国家,再降序排序
need_data = world_data[['name','total_dead']][world_data['total_dead'] >1000]
rank = need_data[['name','total_dead']].sort_values(by='total_dead',ascending=False).values
In [13]:
#导入Pie
from pyecharts.charts import Pie

pie = Pie().add("累计死亡人数", # 添加提示框标签
                rank, # 输入数据
                radius = ["20%", "80%"],  # 设置内半径和外半径
                center = ["60%", "60%"],  # 设置圆心位置
                rosetype = "radius")   # 玫瑰图模式,通过半径区分数值大小,角度大小表示占比

pie.set_global_opts(title_opts = opts.TitleOpts(title="世界国家累计死亡人数玫瑰图",  # 设置图标题
                                                pos_right = '40%'),  # 图标题的位置
                    legend_opts = opts.LegendOpts( # 设置图例
                                                orient='vertical', # 垂直放置图例
                                                pos_right="85%", # 设置图例位置
                                                pos_top="15%"))

pie.set_series_opts(label_opts = opts.LabelOpts(formatter="{b} : {d}%")) # 设置标签文字形式为(国家:占比(%))

# 在notebook中进行渲染     
pie.render_notebook()
Out[13]:

3月以来世界国家累计确诊人数动态条形图

In [14]:
alltime_data = pd.read_csv('alltime_world_2020_05_25.csv')

#首先挑选出疫情最为严重的一些国家,并筛选出这些国家的历史疫情数据:
country_list = ['美国', '英国', '意大利', '西班牙', '法国', '巴西', '比利时', '德国', '伊朗','墨西哥', '加拿大', '荷兰', '中国']
need_data = alltime_data[alltime_data['name'].isin(country_list)]
In [15]:
#使用datetime模块生成时间数据,构造时间列表:
from datetime import datetime,timedelta
time_list1 = [(datetime(2020, 3, 1) + timedelta(i)).strftime('%Y-%m-%d') for i in range(31)]
time_list2 = [(datetime(2020, 4, 1) + timedelta(i)).strftime('%Y-%m-%d') for i in range(30)]
time_list3 = [(datetime(2020, 5, 1) + timedelta(i)).strftime('%Y-%m-%d') for i in range(25)]
time_list = time_list1 + time_list2 + time_list3
print(time_list)
['2020-03-01', '2020-03-02', '2020-03-03', '2020-03-04', '2020-03-05', '2020-03-06', '2020-03-07', '2020-03-08', '2020-03-09', '2020-03-10', '2020-03-11', '2020-03-12', '2020-03-13', '2020-03-14', '2020-03-15', '2020-03-16', '2020-03-17', '2020-03-18', '2020-03-19', '2020-03-20', '2020-03-21', '2020-03-22', '2020-03-23', '2020-03-24', '2020-03-25', '2020-03-26', '2020-03-27', '2020-03-28', '2020-03-29', '2020-03-30', '2020-03-31', '2020-04-01', '2020-04-02', '2020-04-03', '2020-04-04', '2020-04-05', '2020-04-06', '2020-04-07', '2020-04-08', '2020-04-09', '2020-04-10', '2020-04-11', '2020-04-12', '2020-04-13', '2020-04-14', '2020-04-15', '2020-04-16', '2020-04-17', '2020-04-18', '2020-04-19', '2020-04-20', '2020-04-21', '2020-04-22', '2020-04-23', '2020-04-24', '2020-04-25', '2020-04-26', '2020-04-27', '2020-04-28', '2020-04-29', '2020-04-30', '2020-05-01', '2020-05-02', '2020-05-03', '2020-05-04', '2020-05-05', '2020-05-06', '2020-05-07', '2020-05-08', '2020-05-09', '2020-05-10', '2020-05-11', '2020-05-12', '2020-05-13', '2020-05-14', '2020-05-15', '2020-05-16', '2020-05-17', '2020-05-18', '2020-05-19', '2020-05-20', '2020-05-21', '2020-05-22', '2020-05-23', '2020-05-24', '2020-05-25']
In [16]:
day = time_list[0]
draw_data = need_data[need_data['date']==day][['name','total_confirm']].sort_values(by='total_confirm',ascending=True)

print(draw_data)
print(draw_data['name'].replace(contry_name['中文'].values ,contry_name['英文'].values))
      name  total_confirm
9603   比利时              2
10533  墨西哥              5
4355    荷兰             10
8447    英国             36
9063   西班牙             74
872     美国             89
778     德国            129
8103    法国            130
1196    伊朗            978
8562   意大利           1694
171     中国          80174
9603            Belgium
10533            Mexico
4355        Netherlands
8447     United Kingdom
9063              Spain
872       United States
778             Germany
8103             France
1196               Iran
8562              Italy
171               China
Name: name, dtype: object
In [17]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['figure.dpi'] = 100
In [18]:
#设置颜色
color_list = ['brown','peru','orange','blue','green',
              'red','yellow','teal','pink','orchid', 'cornflowerblue', 'darkslategray', 'lightcyan']
country_color = pd.DataFrame()
country_color['country'] = country_list
country_color['color'] = color_list
In [19]:
#定义绘图函数
import matplotlib.ticker as ticker

def barh_draw(day):
    
    # 提取每一天的数据
    draw_data = need_data[need_data['date']==day][['name','total_confirm']].sort_values(by='total_confirm',ascending=True)
    
    # 清空当前的绘图
    ax.clear()
    
    # 绘制条形图
    ax.barh(draw_data['name'].replace(contry_name['中文'].values ,contry_name['英文'].values),draw_data['total_confirm'], color=[country_color[country_color['country']==i]['color'].values[0] for i in draw_data['name']])
    
    # 数值标签的间距
    dx = draw_data['total_confirm'].max()/200
    
    # 添加数值标签
    for j, (name, value) in enumerate(zip(draw_data['name'], draw_data['total_confirm'])):
        
        ax.text(value+dx, j, f'{value:,.0f}', size=10, ha='left', va='center')
        
    # 添加日期标签
    ax.text(draw_data['total_confirm'].max()*0.75, 0.4, day, color='#777777',size=40, ha='left')
    
    # 设置刻度标签的格式
    ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))
    
    # 设置刻度的位置
    ax.xaxis.set_ticks_position('top')

    # 设置刻度标签的颜色和大小
    ax.tick_params(axis='x',colors='#777777', labelsize=15)
    
    # 添加网格线
    ax.grid(which='major',axis='x',linestyle='-')
    
    # 添加图标题
    ax.text(0, 13, 'The cumulative number of confirmed cases in countries around the world',size=20, ha='left')
    
    # 去除图边框
    plt.box(False)
    
    # 关闭绘图框
    plt.close()
In [20]:
fig, ax = plt.subplots(figsize=(14, 8))

import matplotlib.animation as animation
from IPython.display import HTML

animator = animation.FuncAnimation(fig, barh_draw, frames=time_list, interval=200)
HTML(animator.to_jshtml())
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/font_manager.py:1331: UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans
  (prop.get_family(), self.defaultFamily[fontext]))
Out[20]:


Once Loop Reflect